#define _CRT_SECURE_NO_WARNINGS 1
#include <string.h>
#include <stdio.h>
int reverse(char* str) {
    while (*str == ' ') {
        str++;
    }
    char* p1 = str;
    char* p2 = str;
    int count = 0;
    while (*(p2 + 1) != ' ' && *(p2+1)!='\0') {
        p2++;
        count++;
    }
    count++;
    while (p1 <= p2) {
        char ch = *p1;
        *p1 = *p2;
        *p2 = ch;
        p1++;
        p2--;
    }
    return count;
}
char* ReverseSentence(char* str) {
    // write code here
    int len = strlen(str);
    if (len <= 1) {
        return str;
    }
    char* p1 = str;
    char* p2 = str;
    int ret = 0;
    int sum = 0;
    while (*p2 != '\0') {
        ret = reverse(str + sum);
        sum += ret;
        p2 += sum;
        while (*p2 == ' ') {
            p2++;
        }
    }
    int left = 0;
    int right = len - 1;
    printf("%s\n", str);
    while (left < right) {
        int tmp = str[left];
        str[left] = str[right];
        str[right] = tmp;
        left++;
        right--;
    }
    return str;
}
int main() {
    char arr[] = "say?  i  can what";
    printf("%s\n", arr);
    char* str = ReverseSentence(arr);
    printf("%s\n", str);
    return 0;
}